import { ElMessage } from "element-plus";

export const groupDict: any = {
    SampleID: "SampleID",
    subType: "SubType",
    treatment: "Treatment",
    gender: "Gender",
};


const API_BASE = import.meta.env.VITE_MEDIA_URL;

export function fullUrl(path: string) {
    if (path) {
        return API_BASE + path;
    } 
    return '';
}   

export async function download(url: string, filename: string) {
    const u = fullUrl(url);
    if (!u) {
        ElMessage.warning('未找到文件地址');
        return;
    }
    try {
        // 同源/已配置 CORS 的情况：直接 fetch → blob 下载
        const res = await fetch(u, { credentials: 'include' });
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const blob = await res.blob();
        const blobUrl = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = blobUrl;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        a.remove();
        URL.revokeObjectURL(blobUrl);
    } catch (err) {
        console.error('download failed, fallback to open:', err);
        // 跨域未允许或其它异常时，回退为新窗口打开，用户可另存
        window.open(u, '_blank', 'noopener,noreferrer');
    }
}

